﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using COM.Data;
using COM.Web;
using SampleApp.Domain;
using SampleApp.Service;
using SampleApp.Web.Service;

namespace SampleApp.Web.SysModule.Controllers
{
    public class HomeController : Controller
    {
        private ISysMenuService sysMenuService = Unity.Instance.GetService<ISysMenuService>();
        private IRuntime runtime = Unity.Instance.GetService<IRuntime>();

        public ActionResult Index()
        {
            //必须登录
            ViewData["Menu"] = this.GetMenuByPersonId("UserCode");// 获取菜单
            return View();
        }

        public ActionResult Start()
        {
            return View();
        }

        /// <summary>
        /// 根据PersonId获取已经启用的菜单
        /// </summary>
        /// <param name="personId">人员的Id</param>
        /// <returns>菜单拼接的字符串</returns>
        public string GetMenuByPersonId(string personId)
        {
                StringBuilder strmenu = new StringBuilder();
                List<SysMenu> menuNeed = this.sysMenuService.GetCurrentUserSysMenuList();
                //拼接菜单的字符串
                if (menuNeed != null && menuNeed.Any())
                {
                    //一级节点
                    var menuid = from item in menuNeed
                                 where (string.IsNullOrEmpty(item.ParentMenuID) ? "root" : item.ParentMenuID) == "root"
                                 orderby item.SortBy
                                 select item;

                    if (menuid != null && menuid.Any())
                    {
                        //二级节点
                        IList<SysMenu> menus = menuNeed.Except(menuid).OrderBy(i => i.SortBy).ToList();
                        foreach (var item in menuid)
                        {
                            strmenu.Append(" <div title=@" + item.MenuName + "@>");
                            string menusString = this.GetMenus(item.MenuID, menus);
                            if (!string.IsNullOrWhiteSpace(menusString))
                            {
                                menusString = "<ul class=@easyui-tree@" + menusString.Substring(3);
                            }
                            strmenu.Append(menusString);
                            strmenu.Append(" </div>");
                        }
                    }
                }
                return strmenu.ToString().Replace('@', '"');
        }

        /// <summary>
        /// 获取树形结构的字符串
        /// </summary>
        StringBuilder strmenu2 = new StringBuilder();
        /// <summary>
        /// 获取树形结构
        /// </summary>
        /// <param name="menuId"></param>
        /// <param name="sysMenus"></param>
        /// <returns></returns>
        protected string GetMenus(string menuId, IList<SysMenu> sysMenus)
        {
            string res = string.Empty;
            var listTree = from item in sysMenus
                           where (string.IsNullOrEmpty(item.ParentMenuID) ? "root" : item.ParentMenuID) == menuId
                           orderby item.SortBy
                           select item;

            if (listTree != null && listTree.Any())
            {//填充数据
                strmenu2.Append("<ul>");
                foreach (var menuItem in listTree)
                {
                    if (false) //item.SysMenu1.Any()还有子菜单项
                    {
                        //strmenu2.Append("<li><span>" + item.Name + "</span> ");
                        //var getmenu = GetMenus(item.Id, sysMenus);
                        //strmenu2.Append(getmenu);
                        //strmenu2.Append("</li>");
                    }
                    else
                    {
                        strmenu2.Append("<li><a href=@#@ rel=@" + menuItem.Url + "@ >" + menuItem.MenuName + "</a></li>");
                    }
                }
                strmenu2.Append("</ul>");
            }
            res = strmenu2.ToString();
            strmenu2.Clear();
            return res;
        }

    }
}